package com.jas.agent.licence;

import java.nio.charset.StandardCharsets;
import java.security.Signature;
import java.util.Base64;

/**
 * @packageName: com.jas.agent
 * @className: LicenceTest
 * @author: yanyuhui y568821795@outlook.com
 * @date: 2024-10-29 10:26
 * @description: 许可证测试
 */
public class LicenceTest {

    public static void main(String[] args) {
        String key = "BDA7G8034X-eyJsaWNlbnNlSWQiOiJCREE3RzgwMzRYIiwibGljZW5zZWVOYW1lIjoiaGF0cmlja3MiLCJsaWNlbnNlZVR5cGUiOiJQRVJTT05BTCIsImFzc2lnbmVlTmFtZSI6IiIsImFzc2lnbmVlRW1haWwiOiIiLCJsaWNlbnNlUmVzdHJpY3Rpb24iOiIiLCJjaGVja0NvbmN1cnJlbnRVc2UiOmZhbHNlLCJwcm9kdWN0cyI6W3siY29kZSI6IklJIiwiZmFsbGJhY2tEYXRlIjoiMjAyNC0xMC0yOSIsInBhaWRVcFRvIjoiMjAyNi0xMC0yOCIsImV4dGVuZGVkIjp0cnVlfSx7ImNvZGUiOiJQQ1dNUCIsImZhbGxiYWNrRGF0ZSI6IjIwMjQtMTAtMjkiLCJwYWlkVXBUbyI6IjIwMjYtMTAtMjgiLCJleHRlbmRlZCI6dHJ1ZX0seyJjb2RlIjoiUFNJIiwiZmFsbGJhY2tEYXRlIjoiMjAyNC0xMC0yOSIsInBhaWRVcFRvIjoiMjAyNi0xMC0yOCIsImV4dGVuZGVkIjp0cnVlfV0sIm1ldGFkYXRhIjoiMDEyMDI0MDQxMlBQQU0wMDAwMDUiLCJoYXNoIjoiNTYzNTEzMjdcLzA6MTU0MjI3NjA3NCIsImdyYWNlUGVyaW9kRGF5cyI6NywiYXV0b1Byb2xvbmdhdGVkIjp0cnVlLCJpc0F1dG9Qcm9sb25nYXRlZCI6dHJ1ZSwidHJpYWwiOmZhbHNlLCJhaUFsbG93ZWQiOnRydWV9-MH3J9u1JkIyKdjnsgVqEJHVX8i3vzRLUBC68WOlrqLJUK0yqaR55E/fw7MNcE3YWPe8XhhiBr/1eESs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BwWVRDMDBhTHJ3UUxEdk5qSzhSNUoxMk03cTFHWjhqNGpHcVV6Yk9nV1VoTFJKUmg1Z0ExUTdYbmUrCjlSdE1BblVVQk9OUjFVQzNMWXNZbjRIZ3VEUGdHYXBDdktEQXZoU2NFbUs2S2JNQkY5eTlTSzc3aHFueWV1VUsKMHBDNWdybWo1TjByUEhYL1gwcmZrMldDaDgwM2dpdnpRWVAyOWdTMzRaeTk2Y3RKeWJaZUU0dklSdWdFT3JKTQpUUHdvR0UwOG5MVjNJOEZwTS9NOWFhZ3R6WE10OC9PaTFMczB3d1RJQUdMTGNJVUVxNDVjcElGa2xzSzFydjFRClRhcDRZT1dTSUN1Nkd0Tis0MFhIVTRxNDRPdFJMQ3ZXR2tWZ002Wk40RlRnQWJseUtYeDdoT1ZSN3V6UlhWcXUKRUE5WHlWVDV3RDBCaWpUSVkrS1Y0RHNFNmNUeXd5OU85Kzl5byszeW82a2k5VDJMeFkrZkRVTlIxY2dvY0tnaApHUmhEREhjQlFZSlMwaGcyZUJUcnFPTUpMUUU3ckV3NC9RQXdNanNiTEVOUENtb3orZjFhVmVUYTk3SGZkaks0CjlkRUczZmRLaDI3dHFGMFRZazRkVlVMUy9CU2FSd2R6UHdoNm4vVkZmeXllMmFzazNFL3QwT2pnTks1TVNRPT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=";
        isKeyValid(key);
    }

    private static boolean isKeyValid(String key) {
        String[] licenseParts = key.split("-");
        if (licenseParts.length != 4) {
            return false;
        }
        final String licenseId = licenseParts[0];
        final String licensePartBase64 = licenseParts[1];
        final String signatureBase64 = licenseParts[2];
        final String certBase64 = licenseParts[3];

        try {
            final Signature sig = Signature.getInstance("SHA1withRSA");
            //sig.initVerify(createCertificate(Base64.getMimeDecoder().decode(certBase64.getBytes(StandardCharsets.UTF_8)), Collections.emptySet(), false));
            final byte[] licenseBytes = Base64.getMimeDecoder().decode(licensePartBase64.getBytes(StandardCharsets.UTF_8));
            sig.update(licenseBytes);
            if (!sig.verify(Base64.getMimeDecoder().decode(signatureBase64.getBytes(StandardCharsets.UTF_8)))) {
                return false;
            }
            final String licenseData = new String(licenseBytes, StandardCharsets.UTF_8);
            return licenseData.contains("\"licenseId\":\"" + licenseId + "\"");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return false;
    }

}
